package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricityPrice;
import org.szgb.console.ht.electricity.mapper.HtElectricityPriceMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricityPriceService {
    private final HtElectricityPriceMapper htElectricityPriceMapper;

    @Autowired
    public HtElectricityPriceService(HtElectricityPriceMapper htElectricityPriceMapper){
        this.htElectricityPriceMapper = htElectricityPriceMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricityPrice get(String id) {
        return htElectricityPriceMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricityPrice> findList(HtElectricityPrice htElectricityPrice) {
        if (htElectricityPrice != null && htElectricityPrice.getPageDto() != null) {
            Page<HtElectricityPrice> page = new Page<>(htElectricityPrice.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricityPrice.setPage(page);
        }
        return htElectricityPriceMapper.findList(htElectricityPrice);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricityPrice htElectricityPrice) {
        return htElectricityPriceMapper.getCount(htElectricityPrice);
    }

    @Transactional()
    public void save(HtElectricityPrice htElectricityPrice) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricityPrice.getId() == null || htElectricityPrice.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricityPrice.setCreatedBy(htElectricityPrice.getUpdatedBy());
            // 添加UUID
            htElectricityPrice.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricityPriceMapper.insert(htElectricityPrice);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricityPriceMapper.update(htElectricityPrice);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricityPriceMapper.delete(ids.split(","));
    }
    
}
